package com.hzf.wxplatform.web.controller.manager.workman;

import com.alibaba.fastjson.util.Base64;
import com.github.wenhao.geohash.GeoHash;
import com.hzf.api.rpc.api.ApiService;
import com.hzf.common.base.BaseController;
import com.hzf.common.util.ByteUtils;
import com.hzf.common.util.WeixinUtil;
import com.hzf.config.dao.model.ConfigGztype;
import com.hzf.config.dao.model.ConfigGztypeExample;
import com.hzf.config.dao.model.ConfigRegion;
import com.hzf.config.dao.model.ConfigRegionExample;
import com.hzf.designer.dao.model.DneedsDesigner;
import com.hzf.designer.dao.model.DneedsDesignerExample;
import com.hzf.ucenter.dao.model.UcenterUser;
import com.hzf.workman.dao.model.WorkmanManinfo;
import com.hzf.workman.dao.model.WorkmanManinfoExample;
import com.hzf.workman.dao.model.WorkmanManinfoJineng;
import com.hzf.wxplatform.common.WxplatformResult;
import com.hzf.wxplatform.common.WxplatformResultConstant;
import com.hzf.wxplatform.common.utils.UcenterUserUtils;
import com.hzf.wxplatform.common.utils.WxGetGpsUtils;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/workmanUser")
public class WorkmanUserController extends BaseController {
    @Autowired
    ApiService apiService;
    @RequestMapping(value="/index")
    private String index(Model model, HttpServletRequest httpServletRequest){
        HttpSession session = httpServletRequest.getSession();
        UcenterUser ucenterUser = new UcenterUser();
        byte[]user =  UcenterUserUtils.getUser(session);
        if(null==user){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
        ucenterUser = (UcenterUser) ByteUtils.ByteToObject(user);
        if(null==ucenterUser){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
        System.out.println(ucenterUser.getAuthInfo());

        model.addAttribute("user", ucenterUser);
        return  thymeleaf("/GZ/GZ_Single");
    }
    @RequestMapping(value="/workmanYaoqing")
    private String workmanYaoqing(Model model, HttpServletRequest httpServletRequest){
        HttpSession session = httpServletRequest.getSession();
        UcenterUser ucenterUser = new UcenterUser();
        byte[]user =  UcenterUserUtils.getUser(session);
        if(null==user){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
        ucenterUser = (UcenterUser) ByteUtils.ByteToObject(user);
        if(null==ucenterUser){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
        System.out.println(ucenterUser.getAuthInfo());

        model.addAttribute("user", ucenterUser);
        return  thymeleaf("/GZ/GZ_YaoqingZujian");
    }
    @RequestMapping(value="/erweimaYaoqing")
    private String erweimaYaoqing(Model model, HttpServletRequest httpServletRequest){
        HttpSession session = httpServletRequest.getSession();
        UcenterUser ucenterUser = new UcenterUser();
        byte[]user =  UcenterUserUtils.getUser(session);
        if(null==user){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
        ucenterUser = (UcenterUser) ByteUtils.ByteToObject(user);
        if(null==ucenterUser){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
        String url = WeixinUtil.urltoDurl("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx7146b48eb53f7a2f&redirect_uri=http://hzf.nat100.top/wxOauth/getOpenid&response_type=code&scope=snsapi_userinfo&state=4#wechat_redirect");
        model.addAttribute("url",url);
        model.addAttribute("user", ucenterUser);
        return  thymeleaf("/YZ/YZ_Single_YaoQing");
    }
    @RequestMapping(value="/workmanInfo")
    public String workmanInfo(Model model,HttpServletRequest httpServletRequest){

        byte[]user =  UcenterUserUtils.getUser(httpServletRequest.getSession());

        if(null==user){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
        UcenterUser ucenterUser = new UcenterUser();
        ucenterUser = (UcenterUser) ByteUtils.ByteToObject(user);
        if(null==ucenterUser){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
        WorkmanManinfoExample workmanManinfoExample = new WorkmanManinfoExample();
        workmanManinfoExample.createCriteria().andUserIdEqualTo(ucenterUser.getId());
        WorkmanManinfo workmanManinfo = apiService.getWorkmanManinfo(workmanManinfoExample);
        byte[] address = UcenterUserUtils.getAddress(ucenterUser.getId());
        ConfigRegion configRegion = new ConfigRegion();
        configRegion = (ConfigRegion) ByteUtils.ByteToObject(address);
        ConfigRegionExample configRegionExample  = new ConfigRegionExample();
        long type = 1;
        configRegionExample.createCriteria().andRegionTypeEqualTo(type).andRegionNumberNotEqualTo(configRegion.getRegionNumber());
        List<ConfigRegion> list = apiService.selectRegion(configRegionExample);
        model.addAttribute("configList",list);
        model.addAttribute("configRegion",configRegion);
        ConfigGztypeExample configGztypeExample = new ConfigGztypeExample();
        List<ConfigGztype> typeList=apiService.selectConfigPtanliExample(configGztypeExample);
        if(null!=workmanManinfo){
            model.addAttribute("workmanManinfo",workmanManinfo);
            return "/GZ/GZ_info";
        }
        model.addAttribute("typeList",typeList);
        return  thymeleaf("/GZ/GZ_infoAdd");
    }
    @RequestMapping(value="/workmanInfoSave",method = RequestMethod.POST)
    @ResponseBody
    public Object workmanInfoSave(Model model, HttpServletRequest request, WorkmanManinfo dneedsDesigner ){
        byte[]user =  UcenterUserUtils.getUser(request.getSession());

        if(null==user){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
        UcenterUser ucenterUser = new UcenterUser();
        ucenterUser = (UcenterUser) ByteUtils.ByteToObject(user);
        if(null==ucenterUser){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }

        String result= WxGetGpsUtils.lngRequest(request,dneedsDesigner.getLat().toString(),dneedsDesigner.getLng().toString());
        JSONObject jsonObject = JSONObject.fromObject(result);
        byte[] xbuff = Base64.decodeFast((String) jsonObject.get("x"));
        byte[] ybuff = Base64.decodeFast((String) jsonObject.get("y"));
        System.out.println( );
        System.out.println(new String(ybuff) );

        dneedsDesigner.setLat( Double.valueOf(new String(ybuff)));
        dneedsDesigner.setLng(Double.valueOf(new String(xbuff)));
        GeoHash geoHash = GeoHash.fromCoordinate(dneedsDesigner.getLat(),dneedsDesigner.getLng());
        //4024744861876082L
        long longValue = geoHash.toLong();
        dneedsDesigner.setGeoCode(longValue);
        System.out.println(result);
        dneedsDesigner.setUserId(ucenterUser.getId());
        dneedsDesigner.setOrderState(0);
        dneedsDesigner.setState(0);
        dneedsDesigner.setSales(0);
        dneedsDesigner= apiService.installWorkmanManinfo(dneedsDesigner);
        System.out.println(dneedsDesigner.getId());
            for(WorkmanManinfoJineng workmanManinfoJineng :dneedsDesigner.getManinfos()){
             if(null!=workmanManinfoJineng){
                 workmanManinfoJineng.setManinfoId(dneedsDesigner.getId());
                 workmanManinfoJineng.setUserId(ucenterUser.getId());
                 apiService.installWorkmanManinfoJineng(workmanManinfoJineng);
             }

            }
            return new WxplatformResult(WxplatformResultConstant.SUCCESS, "资料设置成功");

    }
    public static double bytes2Double(byte[] arr) {
        long value = 0;
        for (int i = 0; i < 8; i++) {
            value |= ((long) (arr[i] & 0xff)) << (8 * i);
        }
        return Double.longBitsToDouble(value);
    }
}
